from django import template
from django.conf import settings


# 实例化一个template Library
register = template.Library()
@register.inclusion_tag('rbac/menu.html')
def menu(request):
    """生成菜单"""

    menu_dict = request.session.get(settings.MENU_SESSION_KEY)

    # 对二级菜单按照order值进行排序
    for index in menu_dict:
        menu_dict[index]['children'] = sorted(menu_dict[index]['children'], key=lambda keys:keys['order'], reverse=False)

    return {'menu_dict': menu_dict}